#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

#include "kcftracker.hpp"

using namespace std;
using namespace cv;

typedef struct{
    int x_min;
    int y_min;
    int x_max;
    int y_max;
} rect_t;

//global variable
Point pre_point, last_point;
bool    start_drag = false;
bool    start_tarck = false;
bool    selection = false;
rect_t rect;
Rect result;
KCFTracker tracker;
Mat frame;
//mouse event
void On_mouse(int event, int x, int y, int flags, void*);

int main(int argc, char** argv)
{
    //setup parameter
    if (argc > 5) return -1;

    bool HOG = true;
    bool FIXEDWINDOW = false;
    bool MULTISCALE = true;
    bool SILENT = true;
    bool LAB = false;

    for(int i = 0; i < argc; i++){
        if ( strcmp (argv[i], "hog") == 0 )
            HOG = true;
        if ( strcmp (argv[i], "fixed_window") == 0 )
            FIXEDWINDOW = true;
        if ( strcmp (argv[i], "singlescale") == 0 )
            MULTISCALE = false;
        if ( strcmp (argv[i], "show") == 0 )
            SILENT = false;
        if ( strcmp (argv[i], "lab") == 0 ){
            LAB = true;
            HOG = true;
        }
        if ( strcmp (argv[i], "gray") == 0 )
            HOG = false;
    }
    // Create KCFTracker object
    tracker = new KCFTracker(HOG, FIXEDWINDOW, MULTISCALE, LAB);
    string videoFileName = "/home/yan/code/tracking/KCFcpp/DJI_0013.avi";
    auto cap = VideoCapture(videoFileName);
    if(!cap.isOpened())
        return 0;
    namedWindow("KcfTrackerWindow",WINDOW_NORMAL);
    resizeWindow("KcfTrackerWindow",1080,960);
    setMouseCallback("KcfTrackerWindow",On_mouse);

    while(1)
    {
        cap >> frame;
        if(frame.empty())
        {
            cout<<"over...";
            break;
        }

        if(selection)
        {
            rectangle(frame,cvPoint(rect.x_min,rect.y_min),cvPoint(rect.x_max,rect.y_max),Scalar(255,0,0),2,1);
        }

        if(start_tarck)
        {
            result = tracker.update(frame);
//            cout<<result.x<<" "<<result.y;
        }

        if(!result.empty())
        {
            rectangle(frame,CvPoint(result.x,result.y),CvPoint(result.x+result.width,result.y+result.height),Scalar(0,0,255),2,1);
        }

        imshow("KcfTrackerWindow",frame);
        auto k = waitKey(33) & 0xff;
        if(k == 27)
        {
            break;
        }
    }

    cap.release();
    destroyAllWindows();
    return 0;
}


void On_mouse(int event, int x, int y, int flags, void*)
{
    if(event == EVENT_LBUTTONDOWN)
    {
        pre_point =  Point(x,y);
        start_drag = true;
    }

    if(start_drag)
    {
        rect.x_min = min(pre_point.x,x);
        rect.y_min = min(pre_point.y,y);
        rect.x_max = max(pre_point.x,x);
        rect.y_max = max(pre_point.y,y);
        selection = true;
    }

    if(event == EVENT_LBUTTONUP)
    {
        start_drag = false;
        selection = false;
        start_tarck = true;
        if(rect.x_max-rect.x_min>0 && rect.y_max-rect.y_min){
//            cout<<rect.x_max-rect.x_min;
            Rect r(rect.x_min,rect.y_min,rect.x_max-rect.x_min,rect.y_max-rect.y_min);
            tracker.init(r,frame);
        }
    }
}

